var flag = 0;//default focus
var PlayerObj;

var g_pid_type_index = new Array();
var g_pids_index = new Array();

var g_program_name = new Array();
var g_program_token = new Array();
var g_program_index = new Array();
var g_program_count = 0;
var g_pid_count = new Array();;

function keyhandle() {
  //window.location = "http://192.168.0.103/ui/orbittv.html";
  //return;
  var links = document.getElementsByTagName("a");
  flagmax = links.length - 1;
  switch (event.keyCode)
  {
    case 40:
      window.location = "http://192.168.0.103:8888/webkit/orbittv.html";
      return;
      flag += 1;
      if (flag>flagmax)
      {
        flag=0;
      }
      document.getElementById("a"+flag).focus() ;
      moveselect("a"+flag);
      break;
    case 38:
      window.location = "http://192.168.0.199:8080/ui/orbittv.html";
      return;
      flag -= 1;
      if (flag<0)
      {
        flag=flagmax;
      } 
      document.getElementById("a"+flag).focus() ;
      moveselect("a"+flag);
      break;
    case 61522:
      try
      {
        program_list_download();
      }
      catch(e)
      {
        alert(e);
      }
      break;
    case 61506:
      window.location = "http://demo.orbit-tv.com/ui/home.html";
      break;
    case 61508:
      window.location = "http://demo.orbit-tv.com/ui/orbittv.html";
      break;
    case 61507:
      //window.location = "http://jquerymobile.com/demos/1.0a4.1/";
      window.location = "http://demo.orbit-tv.com/ui/";
      break;
    case 61509:
      //window.location = "http://beta.html5test.com/index.html";
      window.location = "http://demo.orbit-tv.com/ui/index2.html";
      break;
    case 13:
    	video_play(4);
    	break;
  }
  return false;
}

window.onload = function ol()
{
  document.getElementById("a"+flag).focus();
  try
  {
    player_init();
  }
  catch(e)
  {
    alert(e);
  }
  program_list_download();
}

function program_list_load_name()
{
  var i =0;
  for(i = 0; i < g_program_count; i++)
  {
    document.getElementById("a"+i).firstChild.innerHTML = g_program_name[g_program_index[i]];
  }

}

function parse_json(json_obj)
{
  var pid_num = 0;
  var i = 0;
  var j = 0;
  var index = 0;
  var program_obj = json_obj.streamdata[1];
  var pid_obj = json_obj.streamdata[0];

  g_program_count = program_obj.programinfo.length;

  for(i = 0; i < g_program_count; i++)
  {
    index = pid_obj.pidinfo[i].index;
    pid_num = pid_obj.pidinfo[i].pids.length;
    var pid_type = new Array();
    var pid_value = new Array();

    for(j = 0; j < pid_num; j++)
    {
      pid_type[j] = pid_obj.pidinfo[i].pids[j].type;
      pid_value[j] = pid_obj.pidinfo[i].pids[j].pid;

    }

    g_pid_type_index[index] = pid_type;
    g_pids_index[index] = pid_value;
    g_pid_count[index] = j;

    g_program_index[i] = program_obj.programinfo[i].index;
    g_program_token[g_program_index[i]] = program_obj.programinfo[i].token;
    g_program_name[g_program_index[i]] = program_obj.programinfo[i].name;
  }

  program_list_load_name();
}

function program_list_download()
{
  var xmlhttp;

  if (window.XMLHttpRequest)// code for IE7+, Firefox, Chrome, Opera, Safari
  {
    xmlhttp=new XMLHttpRequest();
  }
  else// code for IE6, IE5
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open('GET',"/programinfo.json",false);
  xmlhttp.onreadystatechange = make_handle_response(xmlhttp,parse_json);
  xmlhttp.send();
}

function make_handle_response(req,callback)
{
  return function()
  {
    load_program_pids(req,callback);
  };
}

function load_program_pids(xmlhttp,callback)
{
  if (xmlhttp.readyState == 4)
  { 
    if (xmlhttp.status == 200)
    {
      var txt = xmlhttp.responseText;
      //var obj = (JSON && typeof JSON.parse != "undefined") ? JSON.parse(txt) : eval('('+ txt +')');
      var obj = eval('('+ txt +')');
      callback(obj);
      return;
    } 
  }
}

function player_init()
{
  PlayerObj = document.getElementById("pluginobj");
  PlayerObj.SetVideoType("Anyplex");
  PlayerObj.SetVideoPosition(55, 72);
  PlayerObj.SetVideoSize(420, 236);
  PlayerObj.RefreshWindow();

}

function moveselect(x)
{
  var posy = document.getElementById(x).firstChild.offsetTop;
  var posx = document.getElementById(x).firstChild.offsetLeft;
  //var posz = document.getElementById(x).firstChild.z-index;
  posx += 485;
  posy += 71;
  //alert(posz);
  document.getElementById("selectbar").style.top = posy;
  document.getElementById("selectbar").style.left = posx;
}

var t1;

function check_status()
{
  if (!PlayerObj)
  {
    clearInterval(t1);
    return;
  }
  if(PlayerObj.GetPlayStatus() == 2)
  {
    document.getElementById("loading").style.background = "";
    clearInterval(t1);
  }
  else
  {
    //document.getElementById("loading").style.background = "url(img/load.gif) no-repeat";
  }
}

function display_loading()
{
  document.getElementById("loading").style.background = "url(img/load.gif) no-repeat";
  t1 = setInterval("check_status()",1000);
}

function video_play(program_num)
{
  var program_index = g_program_index[program_num];
  var pid_num = g_pid_count[program_index];
  var token = g_program_token[program_index];

  display_loading();

  try
  {
    PlayerObj.Stop();
  }
  catch(e)
  {
    alert(e);
  }

  switch(pid_num)
  {
    case 3:
      PlayerObj.SetVideoPidType(g_pid_type_index[program_index][0], g_pid_type_index[program_index][1], g_pid_type_index[program_index][2]);
      PlayerObj.SetVideoPidValue(g_pids_index[program_index][0], g_pids_index[program_index][1], g_pids_index[program_index][2]);
      break;

  }
	alert("token="+token);
  PlayerObj.Play(token);

}
